package com.springframework.t;

import com.springframework.t.bean.UserDaoImpl;
import com.springframework.t.person.Student;
import com.springframework.t.person.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPackage {
    public static void main(String[] args) {
        //        加载spring配置文件，对象创建
//        ApplicationContext context = new ClassPathXmlApplicationContext("/com/springframework/t/bean.xml.xml");
        ApplicationContext context = new ClassPathXmlApplicationContext("com.springframework/t/bean.xml");
//        获取创建的对象
        User user = (User) context.getBean("user");
        System.out.println(user);

//        使用对像调用方法进行测试
        user.add();
//        根据id获去bean
        Student student = (Student) context.getBean("student");
        System.out.println("根据id获取bean");
        student.eat();
//        根据类型获取bean
//        用类型获取bean的时候只能bean的xml文件只能配置一个id的名字
        Student student2 = (Student) context.getBean(Student.class);
        System.out.println("根据类型获取bean");
        student2.eat();
//        根据id和类型获取bean
        Student student3 = (Student) context.getBean("student",Student.class);
        System.out.println("根据类型和id获取bean");
        student3.eat();

//        接口多实现
        ApplicationContext context2 = new ClassPathXmlApplicationContext("com.springframework/DI_t/bean.xml");
        UserDaoImpl userDao = (UserDaoImpl) context2.getBean(UserDaoImpl.class);
        userDao.run();
    }
}
